describe("TokenSMSController", function(){
	var tokenSMSController = null;

	beforeEach(function(){
		var t_appCtrl = App.ApplicationController.create();
		var t_alertCtrl = App.AlertController.create();
		var t_loadingCtrl = App.LoadingController.create();

		App.TokenSMSController.reopen({
			init:function(){
				this.set('controllers.application', t_appCtrl);
				this.set('controllers.alert', t_alertCtrl);
				this.set('controllers.loading', t_loadingCtrl);
			}
		});

		tokenSMSController = App.TokenSMSController.create();

		// spyOn(App.User,  'login');
		// spyOn(App.User,  'logout');
		
		App.User.reopenClass({
			login : function(userName, pswd, cpch, callback) {},
			logout : function(callback){}
		});
		
		App.getSession().setAttribute('user', App.User.create());
	});

	describe("prepareController", function(){

		it("When customer access sms page, controller can call web service", function(){
			spyOn(App.api_accounts,  'productList');
			spyOn(tokenSMSController,  'showLoading');

			tokenSMSController.prepareController();

			expect(tokenSMSController.get('canResendTimeoutId')).toBe(null);
			expect(tokenSMSController.showLoading).toHaveBeenCalled();
			expect(App.api_accounts.productList).toHaveBeenCalled();
		});

	});
	
	describe("populateProducts", function(){

		it("When customer access sms page, controller will build list", function(){
			spyOn(App.getSession(),  'getProductList').andReturn({
				iHave: {
					list: [],
					totalBalance: 0
				},
				iOwe: {
					list: [],
					totalBalance: 0
				}
			});

			tokenSMSController.populateProducts();

			expect(tokenSMSController.get('iHaveListObj.list')).toEqual([]);
			expect(tokenSMSController.get('iHaveListObj.totalBalance')).toEqual(0);
			expect(tokenSMSController.get('iOweListObj.list')).toEqual([]);
			expect(tokenSMSController.get('iOweListObj.totalBalance')).toEqual(0);
		});

	});
	
	describe("callback", function(){

		it("If without exception, page can show product list", function(){
			spyOn(tokenSMSController,  'populateProducts');
			spyOn(tokenSMSController,  'hideLoading');

			tokenSMSController.callback();

			expect(tokenSMSController.populateProducts).toHaveBeenCalled();
			expect(tokenSMSController.get('canResendTimeoutId')).toBeDefined();
			expect(tokenSMSController.hideLoading).toHaveBeenCalled();
		});
		
		it("If have some exception, page cann't show product list", function(){
			var t_exp = new BreezeException({
	        	errorCode: 123,
	        	errorMessage: 'error'
	        });
	        spyOn(tokenSMSController,  'populateProducts');
			spyOn(tokenSMSController,  'hideLoading');

			tokenSMSController.callback(t_exp);

			expect(tokenSMSController.get('canResendTimeoutId')).toBe(null);
			expect(tokenSMSController.populateProducts).not.toHaveBeenCalled();
			expect(tokenSMSController.hideLoading).toHaveBeenCalled();
		});

	});

	describe("submitPIN", function(){

		it("If submit button can click, controller can call web service for validate 2FAToken", function(){
			tokenSMSController.set('smsToken', '123456');
			tokenSMSController.set('submitBtnDisabled', false);
		
			spyOn(tokenSMSController,  'showLoading');
			spyOn(App.api_alp,  'validate2FAToken');

			tokenSMSController.submitPIN();

			expect(tokenSMSController.showLoading).toHaveBeenCalled();
			expect(App.api_alp.validate2FAToken).toHaveBeenCalled();
		});
		
		it("If submit button cann't click, controller cann't call web service for validate 2FAToken", function(){
			tokenSMSController.set('submitBtnDisabled', true);
		
			spyOn(tokenSMSController,  'showLoading');
			spyOn(App.api_alp,  'validate2FAToken');

			tokenSMSController.submitPIN();

			expect(tokenSMSController.showLoading).not.toHaveBeenCalled();
			expect(App.api_alp.validate2FAToken).not.toHaveBeenCalled();
		});
		
	});

	describe("validate2FATokenCallback", function(){

		it("If without exception, set user to be validated and transition to 'see'", function(){
			var t_twoFA = {};
			var t_exp = null;

			spyOn(tokenSMSController,  'hideLoading');
			spyOn(tokenSMSController,  'transitionTo');

			tokenSMSController.validate2FATokenCallback(t_twoFA, t_exp);

			expect(App.getSession().getAttribute('user').get('isValidated')).toBeTruthy();
			expect(tokenSMSController.hideLoading).toHaveBeenCalled();
			expect(tokenSMSController.transitionTo).toHaveBeenCalled();
			expect(tokenSMSController.get('smsToken')).toBe(null);
			expect(tokenSMSController.get('submitBtnDisabled')).toBeTruthy();
		});

		it("If with exception, set user to be validated and transition to 'see'", function(){
			var t_twoFA = {};
			var t_exp = new BreezeException({
				errorCode: 123,
				errorMessage: 'error'
			});

			spyOn(tokenSMSController,  'hideLoading');

			tokenSMSController.validate2FATokenCallback(t_twoFA, t_exp);

			expect(App.getSession().getAttribute('user').get('isValidated')).toBeFalsy();
			expect(tokenSMSController.hideLoading).toHaveBeenCalled();
			expect(tokenSMSController.get('smsToken')).toBe(null);
			expect(tokenSMSController.get('submitBtnDisabled')).toBeTruthy();
		});

	});
	
	describe("resendPIN", function(){

		it("If resend button can click, controller can call web service for resend OTP", function(){
			tokenSMSController.set('resendBtnDisabled', false);
		
			spyOn(tokenSMSController,  'showLoading');
			spyOn(App.api_alp,  'resendOTP');

			tokenSMSController.resendPIN();

			expect(tokenSMSController.showLoading).toHaveBeenCalled();
			expect(App.api_alp.resendOTP).toHaveBeenCalled();
		});
		
		it("If resend button cann't click, controller cann't call web service for resend OTP", function(){
			tokenSMSController.set('resendBtnDisabled', true);
		
			spyOn(tokenSMSController,  'showLoading');
			spyOn(App.api_alp,  'resendOTP');

			tokenSMSController.resendPIN();

			expect(tokenSMSController.showLoading).not.toHaveBeenCalled();
			expect(App.api_alp.resendOTP).not.toHaveBeenCalled();
		});
		
	});
	
	describe("resendOTPCallback", function(){

		it("If without exception, set resend button to disabled", function(){
			var t_exp = null;

			spyOn(tokenSMSController,  'hideLoading');

			tokenSMSController.resendOTPCallback(t_exp);

			expect(tokenSMSController.get('resendBtnDisabled')).toBeTruthy();
			expect(tokenSMSController.hideLoading).toHaveBeenCalled();
		});

		it("If with exception, donn't set resend button to disabled", function(){
			var t_exp = new BreezeException({
				errorCode: 123,
				errorMessage: 'error'
			});
			tokenSMSController.set('resendBtnDisabled', false);

			spyOn(tokenSMSController,  'hideLoading');

			tokenSMSController.resendOTPCallback(t_exp);

			expect(tokenSMSController.get('resendBtnDisabled')).toBeFalsy();
			expect(tokenSMSController.hideLoading).toHaveBeenCalled();
		});

	});
	
	describe("_clearSMSToken", function(){

		it("controller can set smsToken and submitBtnDisabled and resendBtnDisabled to default", function(){
			tokenSMSController._clearSMSToken();

			expect(tokenSMSController.get('smsToken')).toBe(null);
			expect(tokenSMSController.get('submitBtnDisabled')).toBeTruthy();
			expect(tokenSMSController.get('resendBtnDisabled')).toBeTruthy();
		});

	});
});
